package com.kuaiditu.assistant.ui;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSONObject;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantResponse;
import com.kuaiditu.assistant.base.BaseActivity;
import com.kuaiditu.assistant.module.retrofit.ProgressSubscriber;
import com.kuaiditu.assistant.ui.authority.AuthorityActivity;
import com.kuaiditu.assistant.util.RxCountDown;
import com.kuaiditu.assistant.util.ToastUtil;
import com.kuaiditu.assistant.view.topbar.TopBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DefaultObserver;

@Route(path = RegisterActivity.PATH)
public class RegisterActivity extends BaseActivity {

    public static final String PATH = "/user/register";

    @BindView(R.id.top_bar)
    TopBar topBar;
    @BindView(R.id.edit_mobile)
    EditText editMobile;
    @BindView(R.id.edit_sms_code)
    EditText editSmsCode;
    @BindView(R.id.tv_send_sms_code)
    TextView tvSendSmsCode;
    @BindView(R.id.btn_next)
    Button btnNext;

    AssistantApi mAssistantApi;

    @Override
    public int initContentView() {
        return R.layout.activity_register;
    }

    @Override
    public void initInjector() {
        mAssistantApi = getApplicationComponent().getApiManager()
                .getAssistantApi();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        topBar.bind(this, true);
    }


    @OnClick(R.id.tv_send_sms_code)
    public void onTvSendSmsCodeClicked(View v) {
        String mobile = editMobile.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showToast("手机号不能为空");
            return;
        }
        sendSmsCode((TextView) v,mobile);

    }

    @OnClick(R.id.btn_next)
    public void onBtnNextClicked() {
        String mobile = editMobile.getText().toString();
        String ramdon = editSmsCode.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showToast("手机号不能为空");
            return;
        }
        if (TextUtils.isEmpty(ramdon)) {
            ToastUtil.showToast("验证码不能为空");
            return;
        }
        register(mobile, ramdon);
    }


    private void sendSmsCode(final TextView v, String mobile) {
        mAssistantApi.sendSmsCode(mobile, AssistantApi.ACTION_REGISTER)
                .subscribe(new ProgressSubscriber<AssistantResponse>(mContext) {
                    @Override
                    public void onNext(AssistantResponse assistantResponse) {
                        ToastUtil.showToast("短信发送成功");
                        v.setEnabled(false);
                        new RxCountDown()
                                .countdown(60)
                                .subscribe(new DefaultObserver<Integer>() {
                                    @Override
                                    public void onNext(@NonNull Integer integer) {
                                        v.setText(String.format("%ss", integer));
                                    }

                                    @Override
                                    public void onError(@NonNull Throwable e) {
                                        e.getMessage();
                                    }

                                    @Override
                                    public void onComplete() {
                                        v.setText("点击获取验证码");
                                        v.setEnabled(true);
                                    }
                                });
                    }

                    @Override
                    public void onFinish() {

                    }
                });


    }

    private void register(final String mobile, String ramdon) {
        mAssistantApi.register(mobile, ramdon)
                .subscribe(new ProgressSubscriber<JSONObject>(mContext) {
                    @Override
                    public void onNext(JSONObject jsonObject) {
                        setResult(Activity.RESULT_OK, getIntent().putExtra("mobile", mobile));
                        ARouter.getInstance().build(AuthorityActivity.PATH)
                                .navigation();
                        finish();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


}
